//; ---------------------------------------------------------------------------
//; Subroutine to	update the HUD
//; ---------------------------------------------------------------------------

//; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||

//hudVRAM:	macro loc
//		move.l	#($40000000+((loc&$3FFF)<<16)+((loc&$C000)>>14)),d0
//		endm

#include "video.h"
#include "hud.h"

#define locVRAM hudVRAM
#define hudVRAM(loc) vRamLoc=loc; vRamPtr=(long*)((int)vRam + vRamLoc);
long SWAPLONG(long l)
{
    return ( ((l&0xFF000000) >> 8)+((l&0x00FF0000) << 8) + ((l&0x0000FF00) >> 8)+((l&0x000000FF) << 8));
}

int vRamLoc = 0;

extern word *vRam;
long *vRamPtr = (long*)vRam;

extern word f_pause;

word v_itembonus;          //item bonus from broken enemies, blocks etc. (2 bytes)
word v_timebonus;          //time bonus at the end of an act (2 bytes)
word v_ringbonus;
byte f_endactbonus;

word v_lives;              //live counter (or number of lives)
word v_rings;              //ring counter
byte f_timeover;
byte v_lifecount;	   //live counter value

byte f_lifecount;          //live counter update flag
byte f_ringcount;          //ring counter update flag
byte f_scorecount;
byte f_timecount;

long v_score;
//static union
//{
//long v_time;
byte v_timeP[4];
//};
#define v_timemin v_timeP[1]  
#define v_timesec v_timeP[2] 
#define v_timecent v_timeP[3]  

extern bool f_debugmode;

/*
Art_Hud:	incbin	"artunc\HUD Numbers.bin" ; 8x16 pixel numbers on HUD
		even
Art_LivesNums:	incbin	"artunc\Lives Counter Numbers.bin" ; 8x8 pixel numbers on lives counter
		even
*/
extern byte Art_Hud[];
extern byte Art_LivesNums[];

//HUD_Update:
void HUD_Update()
{
    //tst.w	(f_debugmode).w	; is debug mode	on?
    //bne.w	HudDebug	; if yes, branch
    if (f_debugmode == 0)
    {          
        //tst.b	(f_scorecount).w ; does the score need updating?
        //beq.s	@chkrings	; if not, branch

        if (f_scorecount != 0)
        {	
	    //clr.b	(f_scorecount).w
            f_scorecount = 0;

	    //hudVRAM	$DC80		; set VRAM address
            hudVRAM(0xDC80);
		
            //move.l	(v_score).w,d1	; load score
            long score = v_score;

	    //bsr.w	Hud_Score
            Hud_Score(score);
        }

	//@chkrings:
	//tst.b	(f_ringcount).w	; does the ring	counter	need updating?
        //beq.s	@chktime	; if not, branch
        if (f_ringcount != 0)
        {	
            //bpl.s	@notzero
            if (f_ringcount >= 0x80) //<0
            {
	        //bsr.w	Hud_LoadZero	; reset rings to 0 if Sonic is hit
                Hud_LoadZero();
            }
	    
            //@notzero:
	    //clr.b	(f_ringcount).w
            f_ringcount = 0;
  
	    //hudVRAM	$DF40		; set VRAM address
            hudVRAM(0xDF40);
  
	    //moveq	#0,d1
	    //move.w	(v_rings).w,d1	; load number of rings
            word rings = v_rings;
 
	    //bsr.w	Hud_Rings
            Hud_Rings(rings);
        }
	
        //@chktime:
	//tst.b	(f_timecount).w	; does the time	need updating?
        //beq.s	@chklives	; if not, branch
        if (f_timecount != 0)
        {
	    //tst.w	(f_pause).w	; is the game paused?
	    //bne.s	@chklives	; if yes, branch
            if (f_pause == 0)
            {
		//lea	(v_time).w,a1
                long *timePtr = (long*)&(v_timeP);

		//cmpi.l	#(9*$10000)+(59*$100)+59,(a1)+ ; is the time 9:59:59?
                //beq.s	TimeOver	; if yes, branch
		if (*(timePtr++) == ((9*0x10000)+(59*0x100)+59))
                {
                    //TimeOver...
                    //; ===========================================================================

                    //TimeOver:				; XREF: @chktime
		    //clr.b	(f_timecount).w
		    f_timecount = 0;

                    //lea	(v_player).w,a0
		    //movea.l	a0,a2
		    //bsr.w	KillSonic
		    //KillSonic();
                    
                    //move.b	#1,(f_timeover).w
                    f_timeover = 1;
		    
                    //rts	
                    //; ===========================================================================
                }
                else
                {
                    char *timePtr2 = (char*)timePtr;

                    //addq.b	#1,-(a1)	; increment 1/60s counter
                    *(--timePtr2) = *(timePtr2) + 1;

		    //cmpi.b	#60,(a1)	; check if passed 60
                    //bcs.s	@chklives
		    if (*(timePtr2) >= 60)
                    { 
                        //move.b	#0,(a1)
                        *(timePtr2) = 0;

		        //addq.b	#1,-(a1)	; increment second counter
                        *(--timePtr2) = *(timePtr2) + 1;
 
		        //cmpi.b	#60,(a1)	; check if passed 60
                        //bcs.s	@updatetime
                        if (*(timePtr2) >= 60)
                        {
		            //move.b	#0,(a1)
                            *(timePtr2) = 0;

		            //addq.b	#1,-(a1)	; increment minute counter
                            *(--timePtr2) = *(timePtr2) + 1;

		            //cmpi.b	#9,(a1)		; check if passed 9
                            //bcs.s	@updatetime
                            if (*(timePtr2) >= 9)
                            {
                                //move.b	#9,(a1)		; keep as 9
                                *(timePtr2) = 9;
                            }
                        }
                        
	                //@updatetime:
		        //hudVRAM	$DE40
                        hudVRAM(0xDE40); 

		        //moveq	#0,d1
		        //move.b	(v_timemin).w,d1 ; load	minutes
                        byte minutes = v_timemin;
 
		        //bsr.w	Hud_Mins
                        Hud_Mins(minutes);
 
		        //hudVRAM	$DEC0
                        hudVRAM(0xDEC0);
 
		        //moveq	#0,d1
		        //move.b	(v_timesec).w,d1 ; load	seconds
                        byte seconds = v_timesec;
 
		        //bsr.w	Hud_Secs
                        Hud_Secs(seconds);
                    }
	        }
            }
        }
        
        //@chklives:
        //tst.b	(f_lifecount).w ; does the lives counter need updating?
        //beq.s	@chkbonus	; if not, branch
        if (f_lifecount != 0)
        {
	    //clr.b	(f_lifecount).w
            f_lifecount = 0;
                
	    //bsr.w	Hud_Lives
            Hud_Lives(); 
        }

	//@chkbonus:
	//tst.b	(f_endactbonus).w ; do time/ring bonus counters need updating?
        //beq.s	@finish		; if not, branch
	if (f_endactbonus != 0)
        {
            //clr.b	(f_endactbonus).w
            f_endactbonus = 0;

	    //locVRAM	$AE00
            locVRAM(0xAE00);

	    //moveq	#0,d1
	    //move.w	(v_timebonus).w,d1 ; load time bonus
            word timebonus = v_timebonus;

	    //bsr.w	Hud_TimeRingBonus
            Hud_TimeRingBonus(timebonus);
 
	    //moveq	#0,d1
	    //move.w	(v_ringbonus).w,d1 ; load ring bonus
            word ringbonus = v_ringbonus;

	    //bsr.w	Hud_TimeRingBonus
            Hud_TimeRingBonus(ringbonus);

	}
        
        //@finish:
	//rts	
    }
    else
    {
        /*
        HudDebug:				; XREF: HUD_Update
	bsr.w	HudDb_XY
	tst.b	(f_ringcount).w	; does the ring	counter	need updating?
         	
   	beq.s	@objcounter	; if not, branch
		bpl.s	@notzero
		bsr.w	Hud_LoadZero	; reset rings to 0 if Sonic is hit

	@notzero:
		clr.b	(f_ringcount).w
		hudVRAM	$DF40		; set VRAM address
		moveq	#0,d1
		move.w	(v_rings).w,d1	; load number of rings
		bsr.w	Hud_Rings

	@objcounter:
		hudVRAM	$DEC0		; set VRAM address
		moveq	#0,d1
		move.b	(v_spritecount).w,d1 ; load "number of objects" counter
		bsr.w	Hud_Secs
		tst.b	(f_lifecount).w ; does the lives counter need updating?
		beq.s	@chkbonus	; if not, branch
		clr.b	(f_lifecount).w
		bsr.w	Hud_Lives

	@chkbonus:
		tst.b	(f_endactbonus).w ; does the ring/time bonus counter need updating?
		beq.s	@finish		; if not, branch
		clr.b	(f_endactbonus).w
		locVRAM	$AE00		; set VRAM address
		moveq	#0,d1
		move.w	(v_timebonus).w,d1 ; load time bonus
		bsr.w	Hud_TimeRingBonus
		moveq	#0,d1
		move.w	(v_ringbonus).w,d1 ; load ring bonus
		bsr.w	Hud_TimeRingBonus

	@finish:
		rts	
         */
    }
}   //; End of function HUD_Update

//Hud_TilesBase:	dc.b $16, $FF, $FF, $FF, $FF, $FF, $FF,	0, 0, $14, 0, 0
byte Hud_TilesBase[12] = { 0x16, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0x14, 0, 0 };
//Hud_TilesZero:	dc.b $FF, $FF, 0, 0
byte Hud_TilesZero[4] = { 0xFF, 0xFF, 0, 0 };


//; ---------------------------------------------------------------------------
//; Subroutine to	load "0" on the	HUD
//; ---------------------------------------------------------------------------

//; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||


//Hud_LoadZero:				; XREF: HUD_Update
void Hud_LoadZero()
{
    //locVRAM	$DF40
    locVRAM(0xDF40);
    
    //lea	Hud_TilesZero(pc),a2
    //move.w	#2,d2
    //bra.s	loc_1C83E
    Hud_LoadTiles(Hud_TilesZero, 2);

}   //; End of function Hud_LoadZero

//; ---------------------------------------------------------------------------
//; Subroutine to	load uncompressed HUD patterns ("E", "0", colon)
//; ---------------------------------------------------------------------------

//; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||


//Hud_Base:				; XREF: GM_Level; SS_EndLoop; GM_Ending
void Hud_Base()
{
     //lea	($C00000).l,a6    //base vram address
     
     //bsr.w	Hud_Lives
     Hud_Lives();

     //locVRAM	$DC40
     locVRAM(0xDC40);
 
     //lea	Hud_TilesBase(pc),a2
     //move.w	#$E,d2
     Hud_LoadTiles(Hud_TilesBase, 14);

}

//loc_1C83E:				; XREF: Hud_LoadZero
void Hud_LoadTiles(byte *tiles, signed short count)
{
    //lea	Art_Hud(pc),a1
    byte *HudArtPtr = (byte*)Art_Hud;

    //loc_1C842:
    for (; count>=0; count--)
    { 
        //move.w	#$F,d1
        signed short tileSize = 15;

        //move.b	(a2)+,d0
        signed char curTile = *(tiles++);

	//bmi.s	loc_1C85E
        if (curTile >= 0)
        {
	    //ext.w	d0
	    //lsl.w	#5,d0
            word tileOffset = curTile * 32;

	    //lea	(a1,d0.w),a3
            long *HudTile = (long*)&HudArtPtr[tileOffset];

            //loc_1C852:
            for (; tileSize>=0; tileSize--)
            {
		//move.l	(a3)+,(a6)
                *(vRamPtr++) = SWAPLONG(*(HudTile++));
 
	    }   //dbf	d1,loc_1C852
 
        }
        else
        {
            //loc_1C85E:
            for (; tileSize>=0; tileSize--)
            {
		//move.l	#0,(a6)
                *(vRamPtr++) = 0;
 
	    }   //dbf	d1,loc_1C85E

	}   //bra.s	loc_1C858
            
        //loc_1C858:
    }   //dbf	d2,loc_1C842

    //rts	
    //; ===========================================================================


}   //; End of function Hud_Base

//; ===========================================================================


/*; ---------------------------------------------------------------------------
; Subroutine to	load debug mode	numbers	patterns
; ---------------------------------------------------------------------------

; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||


HudDb_XY:				; XREF: HudDebug
		locVRAM	$DC40		; set VRAM address
		move.w	(v_screenposx).w,d1 ; load camera x-position
		swap	d1
		move.w	(v_player+obX).w,d1 ; load Sonic's x-position
		bsr.s	HudDb_XY2
		move.w	(v_screenposy).w,d1 ; load camera y-position
		swap	d1
		move.w	(v_player+obY).w,d1 ; load Sonic's y-position
; End of function HudDb_XY


; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||


HudDb_XY2:
		moveq	#7,d6
		lea	(Art_Text).l,a1

HudDb_XYLoop:
		rol.w	#4,d1
		move.w	d1,d2
		andi.w	#$F,d2
		cmpi.w	#$A,d2
		bcs.s	loc_1C8B2
		addq.w	#7,d2

loc_1C8B2:
		lsl.w	#5,d2
		lea	(a1,d2.w),a3
		move.l	(a3)+,(a6)
		move.l	(a3)+,(a6)
		move.l	(a3)+,(a6)
		move.l	(a3)+,(a6)
		move.l	(a3)+,(a6)
		move.l	(a3)+,(a6)
		move.l	(a3)+,(a6)
		move.l	(a3)+,(a6)
		swap	d1
		dbf	d6,HudDb_XYLoop	; repeat 7 more	times

		rts	
; End of function HudDb_XY2*/

//; ---------------------------------------------------------------------------
//; HUD counter sizes
//; ---------------------------------------------------------------------------
//Hud_100000:	dc.l 100000		; XREF: Hud_Score
//Hud_10000:	dc.l 10000
//Hud_1000:	dc.l 1000		; XREF: Hud_TimeRingBonus
//Hud_100:	dc.l 100		; XREF: Hud_Rings
//Hud_10:		dc.l 10			; XREF: ContScrCounter; Hud_Secs; Hud_Lives
//Hud_1:		dc.l 1			; XREF: Hud_Mins

long Hud_100000 = 100000;
long Hud_10000 = 10000;
long Hud_1000 = 1000;
long Hud_100 = 100;
long Hud_10 = 10;
long Hud_1 = 1;

//; ---------------------------------------------------------------------------
//; Subroutine to	load rings numbers patterns
//; ---------------------------------------------------------------------------

//; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||


//Hud_Rings:				; XREF: HUD_Update
void Hud_Rings(word rings)
{
    //lea	(Hud_100).l,a2
    //moveq	#2,d6
    //bra.s	Hud_LoadArt
    Hud_LoadArt(rings, 2,  &Hud_100);

}   //; End of function Hud_Rings

//; ---------------------------------------------------------------------------
//; Subroutine to	load score numbers patterns
//; ---------------------------------------------------------------------------

//; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||

//Hud_Score:				; XREF: HUD_Update
void Hud_Score(word score)
{
    //lea	(Hud_100000).l,a2
    //moveq	#5,d6
    Hud_LoadArt(score, 5, &Hud_100000);
}

//Hud_LoadArt:
void Hud_LoadArt(signed long textNum, signed long textLen, long *tenMult)
{
    //moveq	#0,d4
    long d4 = 0;

    //lea	Art_Hud(pc),a1
    byte *HudArt = (byte*)Art_Hud;
 
    //Hud_ScoreLoop:
    for (; textLen>=0; textLen--)
    {
        //moveq	#0,d2
        word digitAtPlace = 0;

        //move.l	(a2)+,d3
        long placeVal = *(tenMult++);

        //loc_1C8EC:
        //sub.l	d3,d1
        //bcs.s	loc_1C8F4
        while ( (textNum = (textNum - placeVal)) >= 0 )
        {
            //addq.w	#1,d2
            digitAtPlace = digitAtPlace + 1;
        
	    //bra.s	loc_1C8EC
        }
        //; ===========================================================================

        //loc_1C8F4:
        //add.l	d3,d1
        textNum = textNum + placeVal;

        //tst.w	d2
        //beq.s	loc_1C8FE
        if (digitAtPlace != 0)
        {
            //move.w	#1,d4
            d4 = 1;
        }
    
        //loc_1C8FE:

        //tst.w	d4
        //beq.s	loc_1C92C
        if (d4 != 0)
        {
            //lsl.w	#6,d2
            word digitTileOffset = digitAtPlace * 64;
 
	    //move.l	d0,4(a6)
         
	    //lea	(a1,d2.w),a3
            long *digitTilePixels = (long*)&HudArt[digitTileOffset];

	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
            for (int lp=0; lp<16; lp++)
                *(vRamPtr++) = SWAPLONG(*(digitTilePixels++));

            //loc_1C92C:
	    //addi.l	#$400000,d0
        }   //dbf	d6,Hud_ScoreLoop
	
    }
    //rts	
}   //; End of function Hud_Score



//; ---------------------------------------------------------------------------
//; Subroutine to	load time numbers patterns
//; ---------------------------------------------------------------------------

//; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||


//Hud_Mins:				; XREF: Hud_ChkTime
void Hud_Mins(byte minDig)
{
    //lea	(Hud_1).l,a2
    //moveq	#0,d6
    //bra.s	loc_1C9BA
    loc_1C9BA(minDig, &Hud_1, 0);
}
    //; End of function Hud_Mins


//; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||

//Hud_Secs:				; XREF: Hud_ChkTime
void Hud_Secs(byte secDig)
{
    //lea	(Hud_10).l,a2
    //moveq	#1,d6
    loc_1C9BA(secDig, &Hud_10, 1);
}

//loc_1C9BA:
void loc_1C9BA(signed long textNum, long *tenMult, signed long textLen)
{
    //moveq	#0,d4
    long d4 = 0;

    //lea	Art_Hud(pc),a1
    byte *HudArt = (byte*)Art_Hud;

    //Hud_TimeLoop:
    for (; textLen>=0; textLen--)
    {
        //moveq	#0,d2
        word digitAtPlace = 0;

        //move.l	(a2)+,d3
        long placeVal = *(tenMult++);

        //loc_1C9C4:
        //sub.l	d3,d1
        //bcs.s	loc_1C9CC
        while ( (textNum = (textNum - placeVal)) >= 0 )
        {
            //addq.w	#1,d2
            digitAtPlace = digitAtPlace + 1;
        
	    //bra.s	loc_1C9C4
        }
        //; ===========================================================================

        //add.l	d3,d1
        textNum = textNum + placeVal;

        //tst.w	d2
        //beq.s	loc_1C9D6
        if (digitAtPlace != 0)
        {
            //move.w	#1,d4
            d4 = 1;
        }
    
        //=======DIFF:======
        //loc_1C8FE:
        //tst.w	d4
        //beq.s	loc_1C92C
        //=================

        //loc_1C9D6:
        //lsl.w	#6,d2
        word digitTileOffset = digitAtPlace * 64;
 
	//move.l	d0,4(a6)
         
	//lea	(a1,d2.w),a3
        long *digitTilePixels = (long*)&HudArt[digitTileOffset];

	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
	//move.l	(a3)+,(a6)
        for (int lp=0; lp<16; lp++)
            *(vRamPtr++) = SWAPLONG(*(digitTilePixels++));

	//rts	
    }
}       //; End of function Hud_Secs

//; ---------------------------------------------------------------------------
//; Subroutine to	load time/ring bonus numbers patterns
//; ---------------------------------------------------------------------------

//; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||


//Hud_TimeRingBonus:			; XREF: Hud_ChkBonus
void Hud_TimeRingBonus(signed long textNum)
{
    //lea	(Hud_1000).l,a2
    long *tenMult = &Hud_1000;

    //moveq	#3,d6
    long textLen = 3;

    //moveq	#0,d4
    long d4 = 0;

    //lea	Art_Hud(pc),a1
    byte *HudArt = (byte*)Art_Hud;
   
    //Hud_TimeLoop:
    for (; textLen>=0; textLen--)
    {
        //moveq	#0,d2
        word digitAtPlace = 0;

        //move.l	(a2)+,d3
        long placeVal = *(tenMult++);

        //loc_1CA1E:
        //sub.l	d3,d1
        //bcs.s	loc_1CA26
        while ( (textNum = (textNum - placeVal)) >= 0 )
        {
            //addq.w	#1,d2
            digitAtPlace = digitAtPlace + 1;
        
	    //bra.s	loc_1CA1E
        }
        //; ===========================================================================

        //loc_1CA26:
        //add.l	d3,d1
        textNum = textNum + placeVal;

        //tst.w	d2
        //beq.s	loc_1C9D6
        if (digitAtPlace != 0)
        {
            //move.w	#1,d4
            d4 = 1;
        }
    
        //=======DIFF:======
        //loc_1CA30:
        //tst.w	d4
        //beq.s	Hud_ClrBonus
        //=================

        if (d4 != 0)
        {
            //loc_1C9D6:
            //lsl.w	#6,d2
            word digitTileOffset = digitAtPlace * 64;
 
	    //move.l	d0,4(a6)
         
	    //lea	(a1,d2.w),a3
            long *digitTilePixels = (long*)&HudArt[digitTileOffset];

	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
            for (int lp=0; lp<16; lp++)
                *(vRamPtr++) = SWAPLONG(*(digitTilePixels++));

	    //rts	
        }
        else
        { 
            //Hud_ClrBonus:
	    //moveq	#$F,d5
            long clrCount = 15;

            //Hud_ClrBonusLoop:
	    //move.l	#0,(a6)
            //dbf	d5,Hud_ClrBonusLoop
            for (int lp=0; lp<clrCount; lp++)
                *(vRamPtr++) =0;
        }
    }
}   //; End of function Hud_TimeRingBonus

//; ---------------------------------------------------------------------------
//; Subroutine to	load uncompressed lives	counter	patterns
//; ---------------------------------------------------------------------------

//; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||


//Hud_Lives:				; XREF: Hud_ChkLives
void Hud_Lives()
{
    //hudVRAM	$FBA0		; set VRAM address
    hudVRAM(0xFBA0);
	
    //moveq	#0,d1
    //move.b	(v_lives).w,d1	; load number of lives
    byte lives = v_lives;
    long textNum = lives;

    //lea	(Hud_10).l,a2
    long *tenMult = &Hud_10;

    //moveq	#1,d6
    signed long textLen = 1;

    //moveq	#0,d4
    long d4 = 0;

    //lea	Art_LivesNums(pc),a1
    byte *LivesNumsArt = (byte*)Art_LivesNums;

    //Hud_LivesLoop:
    for (; textLen>=0; textLen--)
    {
        //moveq	#0,d2
        word digitAtPlace = 0;

        //move.l	(a2)+,d3
        long placeVal = *(tenMult++);

        //loc_1CA90:
        //sub.l	d3,d1
        //bcs.s	loc_1CA98
        while ( (textNum = (textNum - placeVal)) >= 0 )
        {
            //addq.w	#1,d2
            digitAtPlace = digitAtPlace + 1;
        
	    //bra.s	loc_1CA90
        }
        //; ===========================================================================

        //loc_1CA26:
        //add.l	d3,d1
        textNum = textNum + placeVal;

        //tst.w	d2
        //beq.s	loc_1CAA2
        if (digitAtPlace != 0)
        {
            //move.w	#1,d4
            d4 = 1;
        }
    
        //=======DIFF:======
        //loc_1CAA2:
        //tst.w	d4
        //beq.s	Hud_ClrLives
        //=================

        if (d4 != 0)
        {
            //loc_1C9D6:
            //lsl.w	#5,d2
            word digitTileOffset = digitAtPlace * 32;       //DIFF, TILES TWICE AS SMALL
 
	    //move.l	d0,4(a6)
         
	    //lea	(a1,d2.w),a3
            long *digitTilePixels = (long*)&LivesNumsArt[digitTileOffset];

	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
	    //move.l	(a3)+,(a6)
            for (int lp=0; lp<8; lp++)
                *(vRamPtr++) = SWAPLONG(*(digitTilePixels++));

	    //rts	
        }
        else
        { 
            //tst.w	d6
	    //beq.s	loc_1CAA6
            //Hud_ClrLives:
	    //moveq	#7,d5
            long clrCount = 7;

            //Hud_ClrLivesLoop:
	    //move.l	#0,(a6)
            //dbf	d5,Hud_ClrLivesLoop:
            for (int lp=0; lp<clrCount; lp++)
                *(vRamPtr++) =0;
         
            //bra.s	loc_1CABC
        }
        
        //loc_1CABC:
	//addi.l	#$400000,d0
        //dbf	d6,Hud_LivesLoop ; repeat 1 more time
    }
    //rts
//; ===========================================================================
}   //; End of function Hud_Lives
		

